/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.beans;

import bundle.SectorBundle;
import entities.CandidateProfessionalInfo;
import facades.CandidateProfessionalInfoFacade;
import facades.ProvinceFacade;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

/**
 *
 * @author Alvaro
 */
@Named(value = "adminStatisticsBean")
@SessionScoped
public class AdminStatisticsBean implements Serializable {

    private CartesianChartModel categoryModel;
    private CartesianChartModel categoryModelSectors;
    @Inject
    private ProvinceFacade provinceFacade;
    @Inject
    private CandidateProfessionalInfoFacade candidateProInfoFacade;

    @PostConstruct
    public void init() {
        createCategoryModelSectors();
        createCategoryModel();
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public CartesianChartModel getCategoryModelSectors() {
        return categoryModelSectors;
    }

    private void createCategoryModel() {
        categoryModel = new CartesianChartModel();

        ChartSeries cities = new ChartSeries();
        cities.setLabel("Candidatos con disponibilidas geográfica por provincia");
        Vector<Object[]> provincesOfCountry = provinceFacade.getAllProvincesByCountryCandidatesAvailability(209);
        for (Object[] vector : provincesOfCountry) {
            cities.set((String) vector[0], (Long) vector[1]);
        }
        categoryModel.addSeries(cities);

        ChartSeries cities1 = new ChartSeries();
        cities1.setLabel("Candidatos residiendo por provincia");
        Vector<Object[]> provincesOfCountry1 = provinceFacade.getAllProvincesByCountryCandidatesResiding(209);
        for (Object[] vector : provincesOfCountry1) {
            cities1.set((String) vector[0], (Long) vector[1]);
        }
        categoryModel.addSeries(cities1);
    }

    private void createCategoryModelSectors() {
        categoryModelSectors = new CartesianChartModel();
        ChartSeries sectors = new ChartSeries();
        sectors.setLabel("Candidatos por sector");

        List<CandidateProfessionalInfo> allProInfo = candidateProInfoFacade.findAll();
        Map<Object, Number> candidatesPerSector = new HashMap<Object, Number>();

        SectorBundle sBundle = new SectorBundle();
        Enumeration<String> sKeys = sBundle.getKeys();

        for (Enumeration<String> s = sKeys; sKeys.hasMoreElements();) {
            candidatesPerSector.put(sBundle.getString(s.nextElement()), 0);
        }

        for (CandidateProfessionalInfo c : allProInfo) {
            String sectorKey = c.getSector().getId().toString();
            String newDescription = sBundle.getString(sectorKey);
            Number newNumber = candidatesPerSector.get(newDescription).intValue() + 1;
            if (candidatesPerSector.get(newDescription) != null) {
                candidatesPerSector.put(newDescription, newNumber);
            }
        }

        List list = new LinkedList(candidatesPerSector.entrySet());
        Collections.sort(list, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o2)).getValue())
                        .compareTo(((Map.Entry) (o1)).getValue());
            }
        });

        Map result = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            result.put(entry.getKey(), entry.getValue());
        }

        sectors.setData(result);
        categoryModelSectors.addSeries(sectors);
    }

    public String getDatatipFormat() {
        return "<span >%s</span><span style=\"display:none;\">%s</span>";
    }

}
